package com.songcha.library_common.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.ViewConfiguration
import android.widget.Button
import android.widget.FrameLayout
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.ToastUtil
import kotlin.math.absoluteValue


class GestureFrameLayout
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : FrameLayout(context,attrs,defStyle,defRes){
    private var mLastX=0f
    private var mLastY=0f
    private var mListener:IOnGestureFlipListener?=null
    private var mScaledMinimumFlingVelocity=0
    private var mScaledMaximumFlingVelocity=0
    private var mVelocityTracker:VelocityTracker?=null

    enum class GestureFlipDirection{
        LEFT_RIGHT,
        RIGHT_LEFT,
        TOP_BOTTOM,
        BOTTOM_TOP
    }

    interface IOnGestureFlipListener{
        fun onGestureFlip(direction:GestureFlipDirection)
    }

    init{
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        mScaledMinimumFlingVelocity = ViewConfiguration.get(context).scaledMinimumFlingVelocity
        mScaledMaximumFlingVelocity = ViewConfiguration.get(context).scaledMaximumFlingVelocity
    }

    fun setOnGestureFlipListener(listener:IOnGestureFlipListener){
        mListener = listener
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {

        if(ev==null)  return super.dispatchTouchEvent(ev)

        if(mVelocityTracker == null)
            mVelocityTracker = VelocityTracker.obtain()
        mVelocityTracker!!.addMovement(ev)

        when(ev.action){
            MotionEvent.ACTION_DOWN->{
                mLastX = ev.x
                mLastY = ev.y
            }
            MotionEvent.ACTION_UP->{
                var direction = GestureFlipDirection.LEFT_RIGHT
                var isFlip = false
                val deltaX = (ev.x-mLastX).absoluteValue
                val deltaY = (ev.y-mLastY).absoluteValue

                if(deltaY > deltaX){
                    if(ev.y >= mLastY){
                        direction = GestureFlipDirection.TOP_BOTTOM
                    }else{
                        direction = GestureFlipDirection.BOTTOM_TOP
                    }
                }else{
                    if(ev.x >= mLastX){
                        direction=GestureFlipDirection.LEFT_RIGHT
                    }else{
                        direction=GestureFlipDirection.RIGHT_LEFT
                    }
                }

                if(direction == GestureFlipDirection.LEFT_RIGHT || direction == GestureFlipDirection.RIGHT_LEFT){
                    if(mVelocityTracker!=null){
                        mVelocityTracker!!.computeCurrentVelocity(1000,mScaledMaximumFlingVelocity.toFloat())
                        val velocityX=mVelocityTracker!!.xVelocity
                        if(velocityX.absoluteValue>=mScaledMinimumFlingVelocity){
                            isFlip = true
                        }
                    }

                }else{
                    if(mVelocityTracker!=null){
                        mVelocityTracker!!.computeCurrentVelocity(1000,mScaledMaximumFlingVelocity.toFloat())
                        val velocityY=mVelocityTracker!!.yVelocity
                        if(velocityY.absoluteValue>=mScaledMinimumFlingVelocity){
                            isFlip = true
                        }
                    }

                }

                mLastX = 0f
                mLastY = 0f
                mVelocityTracker?.recycle()
                mVelocityTracker?.clear()
                mVelocityTracker = null

                if(isFlip){
                    mListener?.onGestureFlip(direction)
                }
            }

        }

        return super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
    }
}